	$(function() {
		//5分钟超时跳转至订单列表
		setTimeout(function(){
			window.location=rootPath+'/pc/order/list'
		},5*60*1000);
		
		//检查订单支付状态
		setTimeout(function(){
			isOrderPayed();
		},2000);
		
		$("#btn_pay").click(function(){
			var payType = $("div[class='mylabel checked']").attr("payType");
			var url = "";
			var tag = "";
			if (payType == "03") {
				url = rootPath + "/pc/order/publicPay";
				tag = "public";
			} else if (payType == "04") {
				url = rootPath + "/pc/order/privatePay";
				tag = "private";
			}else{
				return false;
			}
			var accountId = $("div[class='mylabel checked']").find("dt[class='checked']").find("input[name='accountId']").val();
			var imgPathList = [];
			$("input[name='"+tag+"ImgPath']").each(function(){
				imgPathList.push($(this).val());
			});
			var imgPath = imgPathList.toString();
			if ((payType == "03" || payType == "04") && (accountId == "" || accountId == undefined)) {
				layer.msg("请选择支付账户",{time:1000});
				return false;
			}
			if ((payType == "03" || payType == "04") && (imgPath == "" || imgPath == undefined)) {
				layer.msg("请上传支付凭证",{time:1000});
				return false;
			}
			if (url == "") {
				layer.msg("请选择支付方式",{time:1000});
				return false;
			}
			var form = $('<form></form>');  
			form.attr('action', url);  
			form.attr('method', 'post');
			form.attr('target', '_self');
			form.append("<input type='hidden' name='orderCode'  value='"+$("#order_code").val()+"'/>");
			form.append("<input type='hidden' name='accountId'  value='"+accountId+"'/>");
			form.append("<input type='hidden' name='imgPath'  value='"+imgPath+"'/>");
			form.appendTo("body");
			form.submit();
		});
		
		$(".bank_list1_con dl dt").each(function(){
			$(this).click(function(){
				$(this).addClass('checked');
				$(this).siblings().removeClass('checked');
			});
		})
		
		$(".mylabel").each(function(){
			$(this).click(function(){
				$("#payDiv").show();
				$(".mylabel").removeClass("checked");
				$(this).addClass("checked");
				//如果没有选择则默认选第一个
				var dtCheck = false;
				$(this).find("dt").each(function(){
					var checked = $(this).attr("class");
					if (checked == 'checked') {
						dtCheck = true;
					}
				});
				if(!dtCheck){
					$(this).find(".bank_list1 dt").eq(0).addClass("checked");
				}
			});
		});;
		
		
		// 支付宝支付
		$("#ali_QR_pay").click(function(){
			var orderCode = $("#order_code").val();
			// 隐藏支付立即按钮
			$("#payDiv").hide();
			$.ajax({
				url:rootPath+"/pc/alipay/swift/payOrder",
				type : 'post',
				data: {orderCode : orderCode},
				dataType: "json",
				success: function(data){
					if(data.prePayStatus == '0'){
						$("#swiftpass_alipay_code").attr("src", data.codeImgUrl);
					}else{
						layer.msg(data.errorMessage,{time:1000});
					}
				},
				error: function(data){ 
					layer.msg(data.errorMessage,{time:1000});
				}
			});
		});
		
		$("#wx_QR_pay").click(function(){
			var orderCode = $("#order_code").val();
			$("#payDiv").hide();
			$.ajax({
				url:rootPath+"/wx/wxpay/swift/payOrder",
				type : 'post',
				data: {orderCode : orderCode},
				dataType: "json",
				success: function(data){
					if(data.prePayStatus == '0'){
						$("#swiftpass_wxpay_code").attr("src", data.codeImgUrl);
					}else{
						layer.msg(data.errorMessage,{time:1000});
					}
				},
				error: function(data){ 
					layer.msg(data.errorMessage,{time:1000});
				}
			});
		});
		
		/**
		$("#wx_QR_pay").click(function(){
			var getQRCode = $("#get_QR_code").val();
			var orderCode = $("#order_code").val();
			$("#payDiv").hide();
			if(getQRCode == "false") {
				$.ajax({
					url:rootPath+"/wx/wxpay/preWxpayQRCode",
					type : 'post',
					data: {orderCode : orderCode},
					dataType: "json",
					success: function(data){
						if(data.resultCode ==1){
							$("#qrcode").qrcode({
								render:"table",
								width:200,
								height:200,
								text:data.QRCode
							});
							$("#get_QR_code").val("true");
						}else{
							layer.msg(data.resultMsg,{time:1000});
						}
					},
					error: function(data){ 
						layer.msg(data.resultMsg,{time:1000});
					}
				});
			}
		});
		*/
		
		//绑定移除按钮
		$("a[name='remove_img_a']").each(function(){
			$(this).on("click", function(){
				$(this).closest("strong").find("input").val("");
				$(this).closest("strong").find("img").attr("src",rootPath+"/statics/images/upload_pic1.png")
			});
		});
	});
	
	//通过ajax提交图片
	function ajaxFileUpload(obj) {
		var id = $(obj).attr("id");
		var tag = $(obj).attr("tag");
		var orderCode = $("#order_code").val();
		var check = imageCheck(obj);
		if (!check) {
			return;
		}
		var index = layer.load(2, {content:'支付凭证正在上传,请耐心等待...',time: 10*1000,shade: 0.15,success: function(layero){
			layero.find('.layui-layer-content')
				.css('padding-top', '10px')
				.css('width','220px')
				.css('padding-left', '40px')
				.css('font-size', '15px');
		}});
		$.ajaxFileUpload({
			url: rootPath + '/uploadFile/uploadOneFile',
			secureuri: false,
			fileElementId: id, // input标签id属性值
			dataType: 'json',
			success: function(data, status) {
				if(data.code == 1) {
					createImage(tag, data.filePath);
//					$("#"+tag+"ImgPath").val(data.filePath);
//					$("#"+tag+"Img").attr("src",imgRootURL+data.filePath+"?x-oss-process=image/resize,h_100/format,png");
					//防止连续上传同一张图片,防止取消上传出现裂图
					$("#"+id).closest("div").find("input[type='file']").val("");
					layer.close(index);
				} else {
					layer.msg('上传失败',{time:1000});
					layer.close(index);
				}
			}
		});
	}
	
	//检查图片格式及大小
	function imageCheck(obj){
		var id = $(obj).attr("id");
		var filePath = $("#"+id).val();
		//检查后缀
		var ext = filePath.substring(filePath.lastIndexOf("."), filePath.length).toUpperCase();
		if (ext != ".BMP" && ext != ".PNG" && ext != ".GIF" && ext != ".JPG" && ext != ".JPEG") {
			layer.msg('只能上传图片',{time:2000});
			return false;
		}
		var file_size = 0;
		if (false || !!document.documentMode) {
			var img = new Image();
			img.src = filepath;
			while (true) {
				if (img.fileSize > 0) {
					if (img.fileSize > 5 * 1024 * 1024) {
						layer.msg('图片不能大于5MB',{time:2000});
						return false;
					}
					break;
				}
			}
		} else {
			file_size = obj.files[0].size;
			var size = file_size / 1024;
			if (size > 5120) {
				layer.msg('图片不能大于5MB',{time:2000});
				return false;
			}
		}
		return true;
	}
	
	//上传图片按钮
	function publicUploadButton(obj,event) {
		$("#publicUpload").click();
	}
	function privateUploadButton(event) {
		$("#privateUpload").click();
	}
	
	function createImage(tag, filePath) {
		var strong = "<strong>" +
				"		<a rel='certImg' href='"+imgRootURL+filePath+"'>" +
				"			<img name='"+tag+"Img' src='"+imgRootURL+filePath+"?x-oss-process=image/resize,h_100/format,png' />" +
				"		</a>" +
				"		<input type='hidden' name='"+tag+"ImgPath' value='"+filePath+"' />" +
				"		<a name='remove_img_a' href='javascript:void(0);' class='icon_close3'></a>" +
				"	</strong>";
		$("#"+tag+"ImgDiv").append(strong);
		$("#"+tag+"ImgDiv").find("a[name='remove_img_a']").each(function(){
			$(this).unbind();
			$(this).click(function(){
				$(this).closest("strong").remove();
				countImage(tag+"ImgDiv");
			});
		});
		countImage(tag+"ImgDiv");
		initFancyBoxImg("certImg");
	}

	//加载图片浏览器
	function initFancyBoxImg(relName){
		$("a[rel='"+relName+"']").fancybox({
			'titlePosition' : 'over',
			'cyclic'        : false,
			'titleFormat'	: function(title) {
				return '';
			}
		});
	}
	
	//图片计数
	function countImage(id) {
		var imgageNum = $("#"+id).find("strong").length;
		if (imgageNum > 0) {
			$("#"+id).find("input[type='button']").val("继续上传");
		} else {
			$("#"+id).find("input[type='button']").val("点击上传");
		}
	}
	
	//订单是否支付
	function isOrderPayed(){
		var orderCode = $("#order_code").val()
		$.ajax({
			url:rootPath+"/pc/order/isOrderPayed",
			type : 'post',
			data: {orderCode : orderCode},
			dataType: "json",
			success: function(data){
				if(data.code == 1){
					window.location.href = rootPath+"/pc/order/paySuccess?orderCode="+orderCode;
				}else{
					//layer.msg(data.resultMsg,{time:2000});
					setTimeout(function(){
						isOrderPayed();
					},2000);
				}
			},
			error: function(data){ 
				layer.msg(data.resultMsg,{time:2000});
			}
		});
	}
	